package com.lsh.payment.core.service.refund.impl;

import com.lsh.base.common.utils.CollectionUtils;
import com.lsh.payment.api.model.refund.RefundQueryCheckRequest;
import com.lsh.payment.core.dao.refund.PayRefundDao;
import com.lsh.payment.core.model.refund.PayRefund;
import com.lsh.payment.core.service.refund.PayRefundOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Project Name: lsh-payment
 * Created by peter on 17/6/28.
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Service
@Transactional(readOnly = true)
public class PayRefundOrderServieImpl implements PayRefundOrderService {

    @Autowired
    private PayRefundDao payRefundDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addRefund(PayRefund payRefund) {
        return payRefundDao.insertSelective(payRefund);
    }

    @Override
    public PayRefund selectRefundByPayRefundNo(String refundNo) {

        List<PayRefund> PayRefunds = payRefundDao.selectByParams(Collections.singletonMap("paymentRefundNo", (Object) refundNo));
        if (CollectionUtils.isEmpty(PayRefunds)) {
            return null;
        }

        return CollectionUtils.getFirst(PayRefunds);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateRefundByPayRefundNo(PayRefund payRefund) {

        return payRefundDao.updateByPayRefundIdSelective(payRefund);
    }


    @Override
    public PayRefund selectRefundByRefundTradeId(String refundTradeId) {

        if(StringUtils.isBlank(refundTradeId)){
            return null;
        }

        List<PayRefund> PayRefunds = payRefundDao.selectByParams(Collections.singletonMap("refundTradeId", (Object) refundTradeId));
        if (CollectionUtils.isEmpty(PayRefunds)) {
            return null;
        }

        return CollectionUtils.getFirst(PayRefunds);
    }

    @Override
    public List<PayRefund> selectRefundByPayPaymentNo(String payPaymentNo) {
        return payRefundDao.selectByParams(Collections.singletonMap("payPaymentNo", (Object) payPaymentNo));
    }



    @Override
    public int updateRefundStatus(PayRefund payRefund) {

        payRefund.setUpdateAt(new Date());

        return payRefundDao.updateByPayRefundIdSelective(payRefund);
    }

    @Override
    public List<Map<String, Object>> queryCheck(RefundQueryCheckRequest refundQueryCheckRequest) {
        return payRefundDao.queryCheck(refundQueryCheckRequest);
    }

}
